package com.zzl.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Title: SendMessageController.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/15 17:30 <br>
 *
 * @author zhuangzhuangliang
 */
@RestController
@Slf4j
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("/rabbit/send/{msg}")
    public String sendMessage(@PathVariable String msg) {
        log.info("当前时间： {},发送一条信息给两个 TTL 队列:{}", new Date(), msg);
        rabbitTemplate.convertAndSend("X","XA","消息来自 ttl 为 10S 的队列"+msg);
        rabbitTemplate.convertAndSend("X","XB","消息来自 ttl 为 40S 的队列"+msg);
        return "ok";
    }


    @RequestMapping("/rabbit/send/{msg}/{ttl}")
    public String sendMessageTTL(@PathVariable("msg") String msg,@PathVariable("ttl") String ttl) {
        log.info("当前时间： {},发送一条信息给两个 TTL 队列:{}", new Date(), msg);
        rabbitTemplate.convertAndSend("X","XC","消息来自 ttl 为 "+ ttl +" 的队列"+msg, message -> {
            message.getMessageProperties().setExpiration(ttl);
            return message;
        });
        return "ok";
    }
}
